﻿using System;
using System.Linq;
using System.Reflection;
using AutoMapper;
using Microsoft.Practices.ObjectBuilder2;

namespace DevWeb.Biztalk.DeploymentSettings.WebUI.Bootstrapper
{
    public class AutoMapperBootstrap
    {

        private static bool IsConfigured = false;

        public static void AutomapperConfigure()
        {

            if (!IsConfigured)
            {
                var profileType = typeof(Profile);
                // Get an instance of each Profile in the executing assembly.
                var profiles = Assembly.GetExecutingAssembly().GetTypes()
                    .Where(t => profileType.IsAssignableFrom(t)
                                && t.GetConstructor(Type.EmptyTypes) != null)
                    .Select(Activator.CreateInstance)
                    .Cast<Profile>();

                // Initialize AutoMapper with each instance of the profiles found.
                Mapper.Initialize(a => profiles.ForEach(a.AddProfile));
                IsConfigured = true;
            }
        }
    }
}